package djboom.materials.display.elements {
	import flash.events.FocusEvent;
	import flash.ui.Keyboard;
	import flash.events.KeyboardEvent;

	import djboom.materials.Work;
	import djboom.materials.core.BitmapDataCore;
	import djboom.materials.events.WindowEvent;
	import djboom.materials.state.Font;

	import org.casalib.display.CasaBitmap;
	import org.casalib.display.CasaShape;
	import org.casalib.display.CasaSprite;
	import org.casalib.display.CasaTextField;

	import flash.display.BlendMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * .
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 23.04.2010
	 * 
	 */

	[Event(name="close", type="djboom.materials.events.WindowEvent")]

	public class WindowDevice extends CasaSprite {

		
		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		private static const WIDTH:int = 580;
		private static const HEIGHT:int = 600;

		private static const PADDING_BORDER:int = 5;		private static const CORNER_RADIUS:int = 12;

		private static const TITLE_TFORMAT:TextFormat = new TextFormat(Font.MYRIAD_PRO_COND, 22, 0x2e3408, true, true);

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _blueShape:CasaShape;
		private var _contentShape:CasaShape;
		private var _simpleButton:RoundedButton;
		private var _titleBitmap:CasaBitmap;
		private var _titleFieldShadow:CasaTextField;
		private var _titleField:CasaTextField;
		private var _deviceBitmap:CasaBitmap;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function WindowDevice() {
			super();
		}	 

		// ----------------------------------------------------------------------------
		// Методы класса.
		// ----------------------------------------------------------------------------

		private function clearContent(event:Event):void {
			this.removeEventListeners();
			
			_simpleButton.destroy();
			_simpleButton = null;
            
			_contentShape.destroy();
			_contentShape = null;
            
			_blueShape.destroy();
			_blueShape = null;
			
			_titleBitmap.destroy();
			_titleBitmap = null;
            
			_titleFieldShadow.destroy();
			_titleFieldShadow = null;
            
			_titleField.destroy();
			_titleField = null;
			
			_deviceBitmap.destroy();
			_deviceBitmap = null;
		}

		private function closeWindow(event:MouseEvent):void {
			var windowEvent:WindowEvent = new WindowEvent(WindowEvent.CLOSE);
			dispatchEvent(windowEvent);
		}

		private function keyDown(event:KeyboardEvent):void {
			if (event.keyCode == Keyboard.ENTER || event.keyCode == Keyboard.ESCAPE || event.keyCode == Keyboard.SPACE) {
				var windowEvent:WindowEvent = new WindowEvent(WindowEvent.CLOSE);
				dispatchEvent(windowEvent);
			}
		}

		private function focusOut(event:FocusEvent):void {
			if (this != null) stage.focus = this;
		}

		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------

		public function init(titleText:String):void {
			
			
			_blueShape = new CasaShape();
			_blueShape.graphics.lineStyle(0.1, 0x00588b, 1, true);
			_blueShape.graphics.beginFill(0x08a7dc);			_blueShape.graphics.drawRoundRect(0, 0, WIDTH, HEIGHT, CORNER_RADIUS);			_blueShape.graphics.endFill();
			_blueShape.alpha = 0.6;
			addChild(_blueShape);
			
			_contentShape = new CasaShape();            
			_contentShape.graphics.beginFill(0xffffff);
			_contentShape.graphics.drawRoundRect(0, 0, WIDTH - PADDING_BORDER * 2, HEIGHT - PADDING_BORDER * 2, CORNER_RADIUS);
			_contentShape.graphics.endFill();
			_contentShape.x = PADDING_BORDER;			_contentShape.y = PADDING_BORDER;
			addChild(_contentShape);
			
			_titleBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(BitmapDataCore.MODAL_WINDOW_TITLE_BIG));
			_titleBitmap.x = PADDING_BORDER;			_titleBitmap.y = PADDING_BORDER;
			addChild(_titleBitmap);
			
			_titleFieldShadow = new CasaTextField();
			_titleFieldShadow.embedFonts = true;
			_titleFieldShadow.selectable = false;
			_titleFieldShadow.defaultTextFormat = TITLE_TFORMAT;
			_titleFieldShadow.autoSize = TextFieldAutoSize.LEFT;
			_titleFieldShadow.antiAliasType = AntiAliasType.ADVANCED;
			_titleFieldShadow.text = titleText;
			_titleFieldShadow.mouseEnabled = false;
			_titleFieldShadow.thickness = 30;
			_titleFieldShadow.blendMode = BlendMode.OVERLAY;
			_titleFieldShadow.textColor = 0xffffff;                      
			_titleFieldShadow.x = PADDING_BORDER + 8;
			_titleFieldShadow.y = PADDING_BORDER + 5;
			_titleFieldShadow.alpha = 0.9;
			addChild(_titleFieldShadow);
            
			_titleField = new CasaTextField();
			_titleField.embedFonts = true;
			_titleField.selectable = false;
			_titleField.defaultTextFormat = TITLE_TFORMAT;
			_titleField.autoSize = TextFieldAutoSize.LEFT;
			_titleField.antiAliasType = AntiAliasType.ADVANCED;
			_titleField.text = titleText;
			_titleField.mouseEnabled = false;
			_titleField.thickness = 30;
			_titleField.x = PADDING_BORDER + 8;
			_titleField.y = PADDING_BORDER + 4;
			_titleField.alpha = 0.9;
			addChild(_titleField);
			
			_deviceBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(BitmapDataCore.LE_CHATELIER_IMAGE));
			_deviceBitmap.x = 58;			_deviceBitmap.y = 68;
			addChild(_deviceBitmap);
			
			_simpleButton = new RoundedButton("   Закрыть   ");
			_simpleButton.x = WIDTH - _simpleButton.width - 30;
			_simpleButton.y = HEIGHT - _simpleButton.height - 30;
			_simpleButton.addEventListener(MouseEvent.CLICK, closeWindow);
			addChild(_simpleButton);
			
			this.x = (Work.WIDTH - WIDTH) * 0.5;
			this.y = (Work.HEIGHT - HEIGHT) * 0.5 - 50;
			
			stage.focus = this;
			
			this.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			this.addEventListener(FocusEvent.FOCUS_OUT, focusOut);            
			addEventListener(Event.REMOVED_FROM_STAGE, clearContent);
		}
        	
			// ----------------------------------------------------------------------------
	        // Публичные свойства.
	        // ----------------------------------------------------------------------------
	}
}
